package se.boars.dailystatistics;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class suggestQuestion extends Activity{
	
	ProgressDialog progressDialog;
	EditText text1;
	EditText text2;
	EditText text3;
	EditText text4;
	JSONParser jParser;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.suggestquestion);
		progressDialog = new ProgressDialog(suggestQuestion.this);
		text1 = (EditText)findViewById(R.id.editText1);
		text2 = (EditText)findViewById(R.id.editText2);
		text3 = (EditText)findViewById(R.id.editText3);
		text4 = (EditText)findViewById(R.id.editText4);
		jParser = new JSONParser();
	}
	
	public void clickHandler(View view) {
		switch(view.getId()) {
		case R.id.button1:
			new submitQuestionTask().execute();
		}
	}
	
	private class submitQuestionTask extends AsyncTask <Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			progressDialog.setMessage("Submittin question for review..");
			progressDialog.show();
		}
		
		@Override
		protected Void doInBackground(Void... arg0) {
			submitQuestion();
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			progressDialog.dismiss();
			AlertDialog.Builder builder = new AlertDialog.Builder(suggestQuestion.this);
			builder.setMessage("Question submitted successfully and will be reviewed!")
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
				}
		});
			AlertDialog alert = builder.create();
			alert.show();
			text1.setText("");
			text2.setText("");
			text3.setText("");
			text4.setText("");
		}
		
	}
	
	public void submitQuestion() {
		ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("tag", "submitQuestion"));
		param.add(new BasicNameValuePair("question", text1.getText().toString()));
		param.add(new BasicNameValuePair("answerA", text2.getText().toString()));
		param.add(new BasicNameValuePair("answerB", text3.getText().toString()));
		param.add(new BasicNameValuePair("answerC", text4.getText().toString()));
		
		jParser.getJSONObjectFromWeb("http://108.174.49.135/rewrite/api.php", param);
	}
}
